/* eslint-disable no-undef */
/* eslint-disable space-before-function-paren */
export const getuser = () => {
  return new Promise((resolve, reject) => {
    wx.getStorage({
      key: 'user',
      success({ data }) {
        if (data) {
          resolve(JSON.parse(data))
        } else {
          resolve({})
        }
      },
      fail() {
        resolve({})
      }
    })
  })
}

export const setuser = (data) => {
  data = JSON.stringify(data)
  return new Promise((resolve, reject) => {
    wx.setStorage({
      key: 'user',
      data,
      success() {
        resolve(data)
        const pages = getCurrentPages()
        const perpage = pages[pages.length - 1]
        perpage.onLoad()
      },
      fail(err) {
        reject(err)
      }
    })
  })
}

export const clearuser = () => {
  return new Promise((resolve, reject) => {
    wx.setStorage({
      key: 'user',
      data: null,
      success() {
        resolve('')
      },
      fail(err) {
        reject(err)
      }
    })
  })
}
